package com.xy.order.controller;

import com.xy.domain.Order;
import com.xy.domain.Product;
import com.xy.order.service.OrderService;
import com.xy.order.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;

//    @GetMapping("/order/prod/{pid}")
//    public Order order(@PathVariable("pid") Integer pid) {
//        log.info("接收到{}号商品的下单请求", pid);
//        Product product =
//                restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
//        //下单
//        Order order = new Order();
//        order.setUid(1);
//        order.setUsername("测试用户");
//        order.setPid(pid);
//        order.setPname(product.getPname());
//        order.setPprice(product.getPprice());
//        order.setNumber(1);
//
//        orderService.createOrder(order);
//        return order;
//    }

//    @GetMapping("/order/prod/{pid}")
//    public Order order(@PathVariable("pid") Integer pid) {
//        log.info("接收到{}号商品的下单请求", pid);
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//        //自定义负载均衡
//        int index = new Random().nextInt(instances.size());
//        ServiceInstance instance = instances.get(index);
//        Product product =
//                restTemplate.getForObject("http://" + instance.getHost() +
//                        ":" + instance.getPort() + "/product/" + pid, Product.class);
//        //下单
//        Order order = new Order();
//        order.setUid(1);
//        order.setUsername("测试用户");
//        order.setPid(pid);
//        order.setPname(product.getPname());
//        order.setPprice(product.getPprice());
//        order.setNumber(1);
//
//        orderService.createOrder(order);
//        return order;
//    }

    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("接收到{}号商品的下单请求", pid);
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        //自定义负载均衡
        int index = new Random().nextInt(instances.size());
        ServiceInstance instance = instances.get(index);
        Product product = productService.findByPid(pid);
//                restTemplate.getForObject("http://service-product/product/" + pid, Product.class);
        //下单
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);

        orderService.createOrder(order);
        return order;
    }
}
